package org.business.accounting.pages;

import java.util.Collection;
import java.util.Vector;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.business.accounting.beans.CartBean;
import org.business.accounting.beans.LogOutHelperBean;
import org.business.accounting.interfaces.Cart;
import org.business.accounting.interfaces.LogOutHelper;
import org.business.accounting.interfaces.User;
import org.business.accounting.service.UserDAO;
/**
 * Start page of application accounting.
 */
@SuppressWarnings("unused")
public class Index {
	
	@ApplicationState
	@Property
	private User user;
	
	@Property
	private boolean userExists;
	
	@ApplicationState
	@Property
	private Cart cart;
	
	private boolean cartExists;
	
	@Inject
	private UserDAO userDAO;
	
	@Persist
	@Property
	private LogOutHelper logOutHelper;
	
	//
	// Geters and Seters
	//
	
	public Collection<User> getUsers() {
		return userDAO.retrieve();
	}
	
	public boolean isAdmin() {
		if (userExists) {
			return user.isAdmin();
		}
		return false;
	}
	
	//
	// Event handlers
	//
	
	@SetupRender
	void onSetupRender() {
		if (!cartExists) {
			cart = new CartBean();
		}
		
		logOutHelper = new LogOutHelperBean();
		logOutHelper.setCart(cart);
		if (userExists) {
			logOutHelper.setUser(user);
		}
	}
}
